package com.ztz.mynewdemo.activity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

import com.ztz.mynewdemo.MessageEvent;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.bean.MyDatabaseHelper;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ScienceActivity extends BaseActivity {
    WebView socWeb;
    public static final String SOCIETY_ID = "id";
    public static final String TITLE = "title";
    public static final String DATE = "date";
    public static final String CATEGORY = "category";
    public static final String AUTHOR = "author";
    public static final String IMAGE = "image";
    @BindView(R.id.btn_collect)
    Button btnCollect;
    MyDatabaseHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_science);
        ButterKnife.bind(this);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        setToolbar("科技");
        socWeb = (WebView) findViewById(R.id.soc_web);
        dbHelper=new MyDatabaseHelper(this,"CollectionStore.db",null,4);
        WebSettings webSettings = socWeb.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        socWeb.setWebViewClient(new WebViewClient());
        String url = getIntent().getStringExtra(SOCIETY_ID);
        final String title = getIntent().getStringExtra(TITLE);
        final String date = getIntent().getStringExtra(DATE);
        final String category = getIntent().getStringExtra(CATEGORY);
        final String author = getIntent().getStringExtra(AUTHOR);
        final String image = getIntent().getStringExtra(IMAGE);
        socWeb.loadUrl(url);
        btnCollect.setOnClickListener(v -> {
            EventBus.getDefault().post(new MessageEvent("你好"));
            SQLiteDatabase db=dbHelper.getWritableDatabase();
            ContentValues values=new ContentValues();
            values.put("type",4);
            values.put("title",title);
            values.put("date",date);
            values.put("category",category);
            values.put("author_name",author);
            values.put("image",image);
            values.put("url",url);
            db.insert("Collection",null,values);
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
